#include "adminwindow.h"
#include "ui_adminwindow.h"

AdminWindow::AdminWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::AdminWindow)
{
    inited = false;
    index = 0;
    ui->setupUi(this);
    // ordertable初始化
    ui->order_table->setHorizontalHeaderLabels(QStringList() << "菜名" << "数量" << "总价" << "备注");
    ui->order_table->setColumnCount(4);
    // 切换标签页面
    connect(ui->TabWidget,&QTabWidget::currentChanged,[=](int i){
        index = 0;
        setList(i);

    });
    // 新增项目回调
    connect(ui->add_dish,&QPushButton::clicked,[=](){
        add();

    });
    connect(ui->add_user,&QPushButton::clicked,[=](){
        add();

    });
    // 点选列表回调
    connect(ui->dish_list,&QListWidget::currentRowChanged,[=](int i){

        qDebug() << "信号切换:" << i;
        const int mode = ui->TabWidget->currentIndex();
        if (i>=0) index = i;
        setContent(mode,i);

    });
    connect(ui->order_list,&QListWidget::currentRowChanged,[=](int i){
        const int mode = ui->TabWidget->currentIndex();
        if (i>=0) index = i;
        setContent(mode,i);

    });
    connect(ui->user_list,&QListWidget::currentRowChanged,[=](int i){
        const int mode = ui->TabWidget->currentIndex();
        if (i>=0) index = i;
        setContent(mode,i);

    });
    // 保存钩子
    connect(ui->dish_save,&QPushButton::clicked,[=](){
        save();

    });
    connect(ui->user_save,&QPushButton::clicked,[=](){
        save();

    });

    // 结束：初始化渲染
    ui->dish_list->setCurrentRow(0);
    ui->TabWidget->setCurrentIndex(0);
//    setList(0);
}

void AdminWindow::setList(int mode)
{
    int i;
    switch (mode) {
    case 0: // 菜品编辑
        ui->dish_list->clear();
        for (auto dish : config.dishList) {
            QListWidgetItem * item = new QListWidgetItem(dish.dishName);
            ui->dish_list->addItem(item);
        }
        if (!inited) ui->dish_list->setCurrentRow(0);
        i = ui->dish_list->currentRow();
        break;
    case 1: // 订单编辑
        ui->order_list->clear();
        for (auto orderlist : config.orderList) {
            QListWidgetItem * item = new QListWidgetItem(orderlist.userName+" 的订单");
            ui->order_list->addItem(item);
        }
        i = ui->order_list->currentRow();
        break;
    case 2: // 用户设置
        ui->user_list->clear();
        for (auto user : config.userList) {
            QListWidgetItem * item = new QListWidgetItem(user.user);
            ui->user_list->addItem(item);
        }
        i = ui->user_list->currentRow();
        break;
    default:
        break;
    }
    inited = true;
    setContent(mode, index);
}

void AdminWindow::setContent(int mode, int i)
{
    if (i < 0) return;
    qDebug() << "设置内容:" << mode<<"." << i;
    switch (mode){
    case 0:
    {
        Dish dish = config.dishList[i];
        ui->dish_name_edit->setText(dish.dishName);
        ui->dish_desc_edit->setPlainText(dish.description);
        ui->dish_price_edit->setValue(dish.price);
        ui->dish_image_edit->setText(dish.imagePath);
        break;
    }
    case 1:
    {
        ui->order_table->clearContents();
        Orderlist userlist = config.orderList[i];
        ui->order_username->setText(userlist.userName);
        ui->order_table->setRowCount(config.orderList.length());

        double allP = 0;
        int row = 0;
        for (Order order : userlist.orderList) {
            Dish dish = config.queryDish(order.name);
            double totalP = order.num * dish.price;
            ui->order_table->setItem(row, 0, new QTableWidgetItem(order.name));
            ui->order_table->setItem(row, 1, new QTableWidgetItem(QString::number(order.num)));
            ui->order_table->setItem(row, 2, new QTableWidgetItem("￥"+QString::number(totalP, 'f', 2)));
            ui->order_table->setItem(row, 3, new QTableWidgetItem(order.info));
            row++;
            allP+=totalP;
        };
        qDebug() << "完毕";
        ui->order_price->setText("￥"+QString::number(allP, 'f', 2));
        break;
    }
    case 2:{
        User user = config.userList[i];
        ui->username_edit->setText(user.user);
        ui->pwd_edit->setText(user.pwd);
        break;
    }
    default:
    {
        break;
    }
    }

}


void AdminWindow::add()
{
    const int mode = ui->TabWidget->currentIndex();
    switch (mode){
    case 0:
    {
        config.dishList.append(Dish("新菜品", "", "", 1.00));
        break;
    }
    case 1:
    {
        // 你不能新建菜单
        return;
    }
    case 2:{
        int userid = 1;
        const QString str = "user";
        while (true)
        {
            QString username = str+QString::number(userid);
            if (config.userNameExist(username)) {
                // 用户名合法，注册新用户
                User user(username, "123456");
                config.userList.push_back(user);
                qDebug() << username;
                break;
            }
            userid++;
        }
        break;
    }
    default:
    {break;}
    }
    setList(mode);
}

void AdminWindow::save()
{
    const int mode = ui->TabWidget->currentIndex();
    switch (mode){
    case 0:
    {
        const QString name = ui->dish_name_edit->text();
        const QString desc = ui->dish_desc_edit->toPlainText();
        const QString image = ui->dish_image_edit->text();
        const double p = ui->dish_price_edit->value();
        config.dishList[index] = Dish(name, desc, image, p);
        break;
    }
    case 1:
    {
        // 你不能新建菜单
        return;
    }
    case 2:{
        auto user = config.userList[index];
        const QString name = ui->username_edit->text();
        const QString pwd = ui->pwd_edit->text();
        if (config.userNameCanChange(name, user.user))
        {
            config.userList[index] = User(name, pwd);
        }
//        config.userList[i] = Dish(name, desc, image, p);
        break;
//        User user = config.userList[index];
//        ui->username_edit->setText(user.user);
//        ui->pwd_edit->setText(user.pwd);
        break;
    }
    default:
    {break;}
    }
    setList(mode);
}



AdminWindow::~AdminWindow()
{
    delete ui;
}
